#include "BackUpSlave.h"
#include "NCManager.h"
#include <pthread.h>
#include <fstream>
#include <iostream>
#include <queue>
#include <math.h>
#include "Domain.h"


using namespace std;

BackUpSlave::BackUpSlave()
{
}

BackUpSlave::~BackUpSlave()
{
}

void * BackUpSlave::backUpJob(void * threadID)
{
	cout << "Ok BU created" << endl;
	/**
	 * Block whatever is possible to block
	 */
	int tts=3000;
	while(true)
	{
	
	sleep(tts);
	try {
	pthread_mutex_lock(&NCManager::mainShip->robotsCounterMutex);
	pthread_mutex_lock(&NCManager::mainShip->pageCounterMutex);
	pthread_mutex_lock(&NCManager::mainShip->domainsMutex);
	pthread_mutex_lock(&NCManager::mainShip->queueMutex);
	pthread_mutex_lock(&NCManager::mainShip->donwloadedSitesMutex);
	
	/**
	 * Saving stats
	 */
	
	time_t now;
	now = time(&now);
	ofstream file;
	file.open("backup/statsReport.log", ios_base::out);
	if(file.is_open())
	{
		file << "Starting Log Report @ " << now << endl;
		file << "########################################" << endl;
		file << " PAGES D/L " << NCManager::visitedSites << endl;
		file << " ROBOTS D/L " << NCManager::fetchedRobots << endl;
		file << " ########################################" << endl;
	//	file << " CANONICAL CONV: " << endl;
	//	file << " \t CALLS: " << CC_calls << " \t TIME: " << CC_time << endl;
	//	file << " HTML FETCHER: " << endl;
	//	file << " \t CALLS: " << HF_calls << " \t TIME: " << HF_time << endl;
	//	file << " LINK PARSER: " << endl;
	//	file << " \t CALLS: " << LP_calls << " \t TIME: " << LP_time << endl;
		file.close();
	}
	tts = sqrt(NCManager::visitedSites)*0.04*60; 
	/**
	 * Finished saving stats
	 */
	
		/**
	 * Saving Domains
	 */
	
	map <string, Domain*>* Dom = &NCManager::mainShip->domains;
	map<string, Domain*>::iterator itr;
	ofstream FD;
	FD.open("backup/Domains.bup", ios_base::out);
	for(itr = Dom->begin();itr!=Dom->end();itr++)
	{
		Domain* D = static_cast<Domain*>(itr->second);
		FD << D->_domain << endl;
		FD << D->_fetchRobots << endl;
		FD << D->_hasRobots << endl;
		FD << D->_whenAvailable << endl;
		//FD << "#" << endl;
	}
	FD.close();
	
	/**
	 * Saving donwloaded sites SET
	 */
	ofstream FS;
	FS.open("backup/Downloaded.bup", ios_base::out);
	set<string>* StrSet = &NCManager::mainShip->downloadedSites;
	set<string>::iterator itrSet;
	if(FS.is_open())
	{
		int setSize = NCManager::mainShip->downloadedSites.size();
		for(itrSet = StrSet->begin();itrSet!=StrSet->end();itrSet++)
			FS << *itrSet << endl;
		FS.close();
	}
	
	
	/**
	 * Saving queues
	 */
	
	//cout << "Tou no BackUpSlave" << endl;
	
	ofstream F1;
	int size;
	F1.open("backup/Queue1.bup", ios_base::out);
	if(F1.is_open()) {
	queue<string>* Q1 = NCManager::mainShip->getVisitQueue1();
	cout << "Fila 1, tamanho: " << Q1->size() << endl;
	size= Q1->size();
	for(int i=0;i < size;i++)
	{
		
		F1 << Q1->front() << endl;
		Q1->push(Q1->front());
		Q1->pop();
	}
	F1.close();
	}
	
	
	ofstream F2;
	F2.open("backup/Queue2.bup", ios_base::out);
	if(F2.is_open()) {
		queue<string>* Q2 = NCManager::mainShip->getVisitQueue2();
		size= Q2->size();
		for(int i=0;i< size;i++)
	{
			F2 << Q2->front() << endl;
			Q2->push(Q2->front());
			Q2->pop();
	}
	F2.close();
	}
	
	
	ofstream F3;pthread_mutex_unlock(&NCManager::mainShip->domainsMutex);
	F3.open("backup/Queue3.bup", ios_base::out);
	if(F3.is_open()) {
		queue<string>* Q3 = NCManager::mainShip->getVisitQueue3();
		size= Q3->size();
		for(int i=0;i< size;i++)
	{
			F3 << Q3->front() << endl;
			Q3->push(Q3->front());
			Q3->pop();
	}
	F3.close();
	}
	
	ofstream F4;
	F4.open("backup/Queue4.bup", ios_base::out);
	if(F4.is_open()) {
		queue<string>* Q4 = NCManager::mainShip->getVisitQueue4();
		size= Q4->size();
		for(int i=0;i< size;i++)
	{
			F4 << Q4->front() << endl;
			Q4->push(Q4->front());
			Q4->pop();
	}
	F4.close();
	}
	
	/**
	 * Saving queues DONE!
	 */
	

	

	
	
	
	pthread_mutex_unlock(&NCManager::mainShip->donwloadedSitesMutex);
	pthread_mutex_unlock(&NCManager::mainShip->queueMutex);
	pthread_mutex_unlock(&NCManager::mainShip->domainsMutex);
	pthread_mutex_unlock(&NCManager::mainShip->pageCounterMutex);
	pthread_mutex_unlock(&NCManager::mainShip->robotsCounterMutex);	
	
	}
	catch(...)
	{
			cout << "Remendo do Cao #1" << endl;
			pthread_mutex_unlock(&NCManager::mainShip->donwloadedSitesMutex);
			pthread_mutex_unlock(&NCManager::mainShip->queueMutex);
			pthread_mutex_unlock(&NCManager::mainShip->domainsMutex);
			pthread_mutex_unlock(&NCManager::mainShip->pageCounterMutex);
			pthread_mutex_unlock(&NCManager::mainShip->robotsCounterMutex);	
			
	}
	
	
	}
	
}
